package com.yanxu.threadpool;

import java.util.concurrent.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2023/11/11-2023
 * @Description: 这里用一句话描述这个类的作用
 */
public class MyThreadPool extends ThreadPoolExecutor {

    public MyThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public MyThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public MyThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public MyThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }


    private boolean isPaused;
    private ReentrantLock reentrantLock = new ReentrantLock();
    private Condition unpaused = reentrantLock.newCondition();

    //线程池中的每一个线程执行之前任务之前需要先执行该方法
    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        System.err.println(Thread.currentThread().getName() + ".......");
        reentrantLock.lock();
        try {
            while (isPaused) {
                System.out.println(" ......");
                unpaused.await();  //线程需要进行 paused ，进行暂停。
            }
        } catch (InterruptedException e) {
            e.printStackTrace(); //线程被 interrupted
        } finally {
            reentrantLock.unlock();
        }
    }

    private void pause() {
        reentrantLock.lock();
        try {
            isPaused = true;
        } finally {
            reentrantLock.unlock();
        }
    }

    private void resume() {
        reentrantLock.lock();
        try {
            isPaused = false;
            unpaused.signalAll(); //唤醒全部的线程
        } finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) {
        MyThreadPool myThreadPool = new MyThreadPool(5, 5, 60l, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
        Runnable task = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "执行了任务!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        for (int i = 0; i < 1000; i++) {
            myThreadPool.submit(task);
        }
    }
}